//
//  JYTTabBarView.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/14.
//  Copyright © 2016年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTTabBarView.h"
#import "JYTTabBarItemView.h"
#import "JYTTabBarItem.h"

@interface JYTTabBarView ()
{
    NSInteger currentIndex;
}
@end

@implementation JYTTabBarView

- (instancetype) init
{
    self = [super init];
    
    NSMutableArray *itemViewArray = [NSMutableArray array];
    
    if (self) {
        currentIndex = 0;
        
        NSNumber *num0 = [[NSNumber alloc] initWithInt:0];
        NSNumber *num1 = [[NSNumber alloc] initWithInt:1];
        NSNumber *num2 = [[NSNumber alloc] initWithInt:2];
        NSNumber *num3 = [[NSNumber alloc] initWithInt:3];
        
        NSArray *itemArray = @[
                               @{@"image":@"tabbar_btn_home@3x.png", @"selectedImage":@"tabbar_btn_home_hl@3x.png", @"index":num0, @"badgeValue":@"0"},
                               
                               @{@"image":@"tabbar_btn_wish@3x.png", @"selectedImage":@"tabbar_btn_wish_hl@3x.png",  @"index":num1, @"badgeValue":@"0"},
                               @{@"image":@"tabbar_btn_travel@3x.png", @"selectedImage":@"tabbar_btn_travel_hl@3x.png",  @"index":num2, @"badgeValue":@"0"},
                               
                               @{@"image":@"tabbar_btn_mine@3x.png", @"selectedImage":@"tabbar_btn_mine_hl@3x.png", @"index":num3, @"badgeValue":@"0"}];
        
        
        for (NSDictionary *dic in itemArray) {
            JYTTabBarItem *item = [[JYTTabBarItem alloc] initWithDic:dic];
            
            CGFloat x, y, width, height;
            
            width = CURRENT_SCREEN_WIDTH / itemArray.count;
            height = TAB_BAR_HEIGHT;
            
            x = item.index * width;
            y = 25;

            
            JYTTabBarItemView *itemView = [[JYTTabBarItemView alloc] initWithtabBarItem:item];
            
            if (item.index == 0) {
                [itemView setHighLightImage];
            }
            
            [self addSubview:itemView];
            
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).with.offset(y);
                make.left.equalTo(self.mas_left).with.offset(x);
                make.width.mas_equalTo(width);
                make.bottom.equalTo(self.mas_bottom).with.offset(0);
            }];
            
            itemView.delegate = self;
            
            [itemViewArray addObject:itemView];
        }
    }
    
    self.tabBarItemArray = itemViewArray;
    self.selectedTabBarItemView = itemViewArray[0];
    
    return self;
}

- (void)clickTabBarItem:(NSInteger)selectedIndex
{
    if (self.delegate && currentIndex != selectedIndex) {
        [self.selectedTabBarItemView setNormalImage];
        [self.delegate clickTabBarItemView:selectedIndex];
        self.selectedTabBarItemView = self.tabBarItemArray[selectedIndex];
        currentIndex = selectedIndex;
    }
}



@end
